from flask import Flask
from flask import request,render_template

app = Flask("hiFlask")

@app.route('/hi',methods=['GET','POST'])
def hi():
    #使用request请求对象获取请求方法
    if request.method=="GET":
        return """
        <h1>用户登录</h1>
        <form  action="/hi" method="post">
        <input name="name" placeholder="用户名"><br>
        <input name="pwd" placeholder="口令"><br>
        <button>提交</button>
        </form>
        """
    else:
        #获取表单的参数
        name = request.form.get('name')
        pwd = request.form.get('pwd')
        if all((
            name == 'disen',
            pwd == '987'
        )):
            return """
            <h2 style="color:blue;">登陆成功</h2>
            """
        else:
            return """
                <h2 style="color:orange;">登陆失败,<a href = "/hi">重试</a></h2>
             """


@app.route('/bankcard001',methods=['GET','POST'])
def addBankCard():
    data = {
        'title':'绑定银行卡',
        'error_message':' '
    }
    if request.method =='POST':
        #处理post请求
        name = request.form.get('name',None)
        card_num = request.form.get("card_num",None)
        if all((name,card_num)):
            app.logger.info('name:%s-->card:%s'%(name,card_num))
            return  """
                <h2>绑定银行卡成功</h2>
                <h4 id ="result"></h4>
                <script>
                    let steps = 5;
                    let interval_id = 0;
                    setInterval(()=>{
                        if(steps>=0){
                            document.getElementById('result').innerText=steps--
                        }else{
                            //取消定时器
                            clearInterval(interval_id);
                            window.open('/hi',target='_self')
                        }
                        
                    },1000)
                                    
                </script>
            """
        data['error_message'] = '银行卡号不能为空'
    return render_template('band_edit.html',**data)

app.run('localhost',5000,True)
